package com.pdx.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

/**
 * @author 派 大 星
 * @function Shiro的第一个例子
 * @date 2022/4/5 16:00
 * @website https://blog.csdn.net/Gaowumao
 */
public class HelloShiro {

    @Test
    public void shiroLogin(){
        //导入权限的ini文件构建权限工厂
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        //工厂构建安全管理器
        SecurityManager instance = factory.getInstance();
        //使用SecurityUtils工具获取主体
        SecurityUtils.setSecurityManager(instance);
        Subject subject = SecurityUtils.getSubject();
        //构建账号
        UsernamePasswordToken token = new UsernamePasswordToken("jay", "123");
        //登陆操作
        subject.login(token);
        System.out.println("是否登陆成功："+subject.isAuthenticated());
    }
}
